from rest_framework.viewsets import ModelViewSet, ViewSet
from apps.users.authentication import CustomAuthentication
from apps.users.permissions import CustomIsStaff
from apps.students.models import Student
from apps.students.serializers import StudentModelSerializer
from rest_framework.response import Response
from rest_framework import status
from apps.students.pagination import StudentPageNumberPagination


# Create your views here.
class StudentsViewSet(ModelViewSet):
    authentication_classes = [CustomAuthentication]
    permission_classes = [CustomIsStaff]
    pagination_class = StudentPageNumberPagination

    # 指明过滤、搜索、支持的排序字段
    filterset_fields = ['sno', 'staff_id']
    search_fields = ['name', 'wechat_nickname']
    ordering_fields = ['id', 'sno']

    queryset = Student.objects.all().order_by('-sno')
    serializer_class = StudentModelSerializer


class StudentsDescViewSet(ViewSet):
    authentication_classes = [CustomAuthentication]
    permission_classes = [CustomIsStaff]

    @staticmethod
    def get_state_info(request):
        states = dict()
        for code, state in Student.STATE_CHOICES:
            states[code] = state
        return Response({"message": "获取学习状态描述信息成功!", "data": states}, status=status.HTTP_200_OK)

    @staticmethod
    def get_sex_info(request):
        sexes = dict()
        for code, sex in Student.SEX_CHOICES:
            sexes[code] = sex
        return Response({"message": "获取学员性别描述信息成功!", "data": sexes}, status=status.HTTP_200_OK)

